/**
 * @format
 * @Author: TC
 * @Date: 2025-06-07 21:52:29
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-07 21:53:12
 */

import { _decorator, Event, EventTouch, Node, UITransform, v3, Vec2, Vec3 } from "cc";
import { BaseComponent } from "db://assets/FrameWork/Module/Component/BaseComponent";
import { EntityHero } from "../../../Game/Entity/EntityHero";
import { gameDataMgr } from "../../../_Common/Manager/GameDataManager";

const { ccclass, property } = _decorator;

/**
 * 射击虚拟摇杆
 */
@ccclass
export default class VirtualJoystickFire extends BaseComponent {
    private bar: Node; //圆环
    private circleRadius: number = 0; //圆环半径
    private touchID: number = -1; //触摸ID

    private p1: Vec3 = Vec3.ZERO;
    private p2: Vec3 = Vec3.ZERO;

    private angle: number = 0;

    private currAngle: number = 0;
    private _char: EntityHero = null;
    private radian: number = 0;

    onLoad() {
        this.init();
    }

    start() {}

    protected init(): void {
        this.bar = this.node.getChildByName("bar");
        this.node.on(Node.EventType.TOUCH_START, this.onTouchBegin, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.circleRadius = this.node.getComponent(UITransform).contentSize.width / 4;
    }

    private onTouchBegin(e: EventTouch) {
        if (this.touchID > -1) return;
        this.touchID = e.getID();
        let uiPos = e.getUILocation();
        let uirf = this.node.getComponent(UITransform);
        this.bar.setPosition(uirf.convertToNodeSpaceAR(v3(uiPos.x, uiPos.y, 0)));

        this.onTouchMove(e);
    }

    private onTouchMove(e: EventTouch) {
        // log(e.getID());
        if (this.touchID != e.getID()) return;

        //获取手指和虚拟摇杆的距离
        let uiPos = e.getUILocation();
        let uirf = this.node.getComponent(UITransform);
        this.p2 = uirf.convertToNodeSpaceAR(v3(uiPos.x, uiPos.y, 0));
        this.radian = Math.atan2(this.p2.y, this.p2.x);
        // console.log(this.radian, (this.radian / Math.PI) * 180);
        var dist: number = this.p2.length();
        //手指距离在圆环范围内
        if (dist <= this.circleRadius) {
            this.bar.setPosition(this.p2);
            //手指距离在圆环范围外
        } else {
            this.bar.setPosition(Math.cos(this.radian) * this.circleRadius, Math.sin(this.radian) * this.circleRadius, 0);
        }
    }

    private onTouchEnd(e: EventTouch) {
        // log(e.getID());
        // if (this.touchID != e.getID())
        //     return;
        // log("up this.touchID=", this.touchID)
        this.bar.setPosition(Vec3.ZERO);
        this.touchID = -1;
        this.angle = 0;
    }

    update() {
        if (!gameDataMgr.entityHero) return;
        this._char = gameDataMgr.entityHero;
        if (-1 == this.touchID) {
            // this.roleControl.removeDis(this.disCode);
            // this.cleanKeys();
            this._char.act.firing = false;
            return;
        }
        const angle: number = (this.radian / Math.PI) * 180;
        this._char.act.updateFireAngle(angle);
        this._char.act.firing = true;
    }
}
